<?php
namespace app\api\controller;

use think\Controller;

class Base extends Controller
{
    protected $TOKEN = '!@#ttylkz54235';
    public $user_id;
    public $com_id;

    public function _initialize(){
//        $this ->__respond2();//请求验证数字签名
        define('MODULE_NAME', request()->module());
        define('CONTROLLER_NAME', request()->controller());
        define('ACTION_NAME', request()->action());
        $this->verify_login();  //验证登录情况
    }

    /***
     * 验证登陆状态
     * @return \think\response\Json
     */
    protected function verify_login(){
        $param = request() ->param();
        $user_id = isset($param['uid']) ? trim($param['uid']) : 1;
        $this->user_id = $user_id;  //设置用户登录的iD值
        $this->com_id = M('users')->where('id',$user_id)->value('com_id');
        // 不需要验证登陆的控制器和方法
        $controller = [
            'user'=>['com_register','forget_password','login','send_code','user_search','get_server','upload_img'],
            'shop'=>['order_pay'],
        ];
        $req_c = strtolower(CONTROLLER_NAME);
        $req_a = strtolower(ACTION_NAME);
        if(array_key_exists($req_c, $controller) && in_array($req_a, $controller[$req_c])){

        }else{
//            if (empty($user_id)){
//                return return_json(-1,'未登录');
//            }
        }


    }

    /**
     * @param $length
     * @return string
     * 用户注册生成的密码盐
     */
    protected function randomkeys($length)
    {
        $key = '';
//        $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
        $pattern = '1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZ';
        for($i=0;$i<$length;$i++)
        {
            $key .= $pattern{mt_rand(0,35)};
        }
        return $key;
    }

    /**
     * @param $url
     * @param int $timeout
     * @param array $options
     * @return mixed
     * @author:tian
     */
    protected function curl_get($url,$timeout=15,$options = array())
    {
        $defaults = array(
            CURLOPT_URL => $url,
            CURLOPT_HEADER => 0,
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLOPT_NOBODY=>false,
            //CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0',
            //CURLOPT_FOLLOWLOCATION=>1,
            CURLOPT_TIMEOUT => $timeout
        );

        $ch = curl_init();
        curl_setopt_array($ch, ($options + $defaults));
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
    /***
     * @return \think\response\Json
     * 安全验证
     */
    protected function __respond2(){
        //验证身份
        $data = request()->param();
        $time = isset($data['t']) ? trim($data['t']) : '';
        $signature = isset($data['sign']) ? trim($data['sign']) : '';
        unset($data['sign']);
        if (!empty($data) && !empty($time) && !empty($signature)){
            $str = $this -> __arithmetic2($data);
            if($str != $signature){
                return return_json(1,'签名错误');
            }
        }else{
            return return_json(1,'参数不全');
        }
    }

    /**
     * @param $timeStamp 时间戳
     * @param $randomStr 随机字符串
     * @return string 返回签名
     */
    protected function __arithmetic2($data){

        $data['token'] = $this ->TOKEN;
        //按照首建值大小写顺序排序
        ksort($data);
        //拼接成字符串
        $str = implode($data);
        //进行加密
        $signature = md5($str);
        //转换成小写
        $signature = strtolower($signature);

        return $signature;
    }
    //验证验证码
    public function code_check($mobile,$code){

        $sms_log = M('send_sms_log')->where('mobile',$mobile)->order('create_time desc')->find();
        if(empty($sms_log)){
            return array(1,'验证码不存在,请重新发送');
        }
        $time = time() - 5*60;
        if(($sms_log['status'] == 1) || ($sms_log['create_time'] < $time)){
            return array(1,'验证码失效');
        }
        if($sms_log['code'] != $code){
            return array(1,'验证码错误');
        }
        //修改状态
        M('send_sms_log')->where('id',$sms_log['id'])->update(['status'=>1]);
        return array(0);
    }
    //时间转换星期
    public function time_change_week($time){
        //获取数字型星期几
        $number_wk=date("w",$time);
//        return $number_wk;
        //自定义星期数组
        $weekArr=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");

        //获取数字对应的星期
        return $weekArr[$number_wk];
    }
    //冒泡排序
    public function data_sort($arr,$key)
    {
        $count = count($arr);
        $temp = 0;
        //外层控制排序轮次
        for ($i = 0; $i < $count - 1; $i++) {
            //内层控制每轮比较次数
            for ($j = 0; $j < $count - 1 - $i; $j++) {
                if ($arr[$j][$key] < $arr[$j + 1][$key]) {
                    $temp = $arr[$j];
                    $arr[$j] = $arr[$j + 1];
                    $arr[$j + 1] = $temp;
                }
            }
        }
        return $arr;
    }
    //获取订单号
    public function get_order_number(){
        $key = '';
        $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
        for($i=0;$i<6;$i++)
        {
            $key .= $pattern{mt_rand(0,9)};
        }
        return time().$key;
    }
    //对象转数组
    public function object_array($array) {
        if(is_object($array)) {
            $array = (array)$array;
        } if(is_array($array)) {
            foreach($array as $key=>$value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }
    //获取部门下级部门
    public function get_department_child($list,$id,$arr=[]){
        $arr[] = $id;
        foreach ($list as $key=>$value){
            if($value['pid'] == $id){
                $arr[] = $value['id'];
                $arr = $this->get_department_child($list,$value['id'],$arr);
            }
        }
        return $arr;
    }

}
